package net.homeftp.heimo.weddingwidget.ui.seekbar;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class SeekBarDialog extends Dialog {

	private OnSeekListener mlistener = null;
	private int defaultSelection = 100;
	private int maxSelection = 10;
	private Context mContext = null;
	private SeekBarDialog seekDialog;

	public SeekBarDialog(Context context, OnSeekListener mlistener, int val, int maxVal) {
		super(context);
		this.mContext = context;
		this.setTitle("Please select a value");
		this.mlistener = mlistener;
		this.maxSelection = maxVal;
		this.defaultSelection = (val>maxVal)?maxVal:val;
		this.seekDialog = this;
	}
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LinearLayout layout = new LinearLayout(mContext);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.CENTER_HORIZONTAL);
		layout.setPadding(6,6,6,6);
		
		SeekBar mSeekBar = new SeekBar(mContext);
		mSeekBar.setProgress(defaultSelection);
		mSeekBar.setMax(maxSelection);
		mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				defaultSelection = progress;
				if (mlistener != null) {
					mlistener.seekProgress(defaultSelection);
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				if (mlistener != null) {
					mlistener.seekValue(defaultSelection);
				}
				seekDialog.dismiss();
			}
		});
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		layout.addView(mSeekBar, params);
		setContentView(layout);
	}
}
